package com.example.test.login;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.test.Admin_function.add_test;
import com.example.test.R;
import com.example.test.Test.all_test;
import com.example.test.all_Myhelper.MyHelper;
import com.example.test.Admin_function.add_tiku;
import com.example.test.exam.all_tiku;

public class admin extends AppCompatActivity implements View.OnClickListener {

    MyHelper ad_myhelper;
    public EditText admin_id;
    public EditText admin_password;
    public TextView xianshi;
    public Button cx;
    public Button del;
    public Button ad_timu,chaxun_timu,add_test,chaxun_test;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.admin);
        ad_myhelper = new MyHelper(this);
        ad_init();

    }

    public void ad_init(){
        admin_id = (EditText)findViewById(R.id.admin_id);
        admin_password = (EditText)findViewById(R.id.admin_password);
        xianshi = (TextView)findViewById(R.id.xianshi);
        cx = (Button)findViewById(R.id.chaxun);
        del = (Button)findViewById(R.id.delete);
        ad_timu = (Button)findViewById(R.id.tianjiatimu);
        chaxun_timu = (Button) findViewById(R.id.chaxun_timu);
        add_test = (Button) findViewById(R.id.admin_add_test);
        chaxun_test = (Button) findViewById(R.id.admin_select_test);
        ad_timu.setOnClickListener(this);
        cx.setOnClickListener(this);
        del.setOnClickListener(this);
        chaxun_timu.setOnClickListener(this);
        add_test.setOnClickListener(this);
        chaxun_test.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {
        String name = null, password=null;
        SQLiteDatabase db;
        ContentValues values;
        if(v.getId()==R.id.chaxun){
                db = ad_myhelper.getReadableDatabase();
                Cursor cursor = db.query("user",null,null,null,null,null,null);
                if(cursor.getCount() == 0){
                    xianshi.setText("");
                    Toast.makeText(this,"没有数据",Toast.LENGTH_SHORT).show();
                }else {
                    cursor.moveToFirst();
                    xianshi.setText("Name :" + cursor.getString(1)+"    password:"+cursor.getString(2));
                    }while (cursor.moveToNext()){
                    xianshi.append("\n"+"Name :" + cursor.getString(1)+"    password:"+cursor.getString(2));
                }
                cursor.close();
                db.close();
        }else if(v.getId()==R.id.delete){
            db = ad_myhelper.getReadableDatabase();
            String na = admin_id.getText().toString();
            String pd = admin_password.getText().toString();
//            db.delete("user",null,null);
            db.delete("user","name=? and password=?",new String[]{na,pd});
            Toast.makeText(this,"信息已删除",Toast.LENGTH_SHORT).show();
            xianshi.setText("");
            db.close();
        }else if(v.getId()==R.id.tianjiatimu){
            Intent enter = new Intent(admin.this, add_tiku.class);
            startActivity(enter);
        }else if(v.getId()==R.id.chaxun_timu){
            Intent find = new Intent(admin.this, all_tiku.class);
            startActivity(find);
        }else if(v.getId()==R.id.admin_select_test){
            Intent admin_s_t = new Intent(admin.this, all_test.class);
            startActivity(admin_s_t);
        }else if(v.getId()==R.id.admin_add_test){
            Intent admin_a_t = new Intent(admin.this, com.example.test.Admin_function.add_test.class);
            startActivity(admin_a_t);
        }
    }
}